home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Modules / _tkinter.c next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  31.5 KB  |  1,481 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright (C) 1994 Steen Lumholt.
  3. Copyright 1994-1995 by Stichting Mathematisch Centrum, Amsterdam,
  4. The Netherlands.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its
  9. documentation for any purpose and without fee is hereby granted,
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in
  12. supporting documentation, and that the names of Stichting Mathematisch
  13. Centrum or CWI or Corporation for National Research Initiatives or
  14. CNRI not be used in advertising or publicity pertaining to
  15. distribution of the software without specific, written prior
  16. permission.
  17.  
  18. While CWI is the initial source for this software, a modified version
  19. is made available by the Corporation for National Research Initiatives
  20. (CNRI) at the Internet address ftp://ftp.python.org.
  21.  
  22. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  23. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  24. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  25. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  26. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  27. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  28. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  29. PERFORMANCE OF THIS SOFTWARE.
  30.  
  31. ******************************************************************/
  32.  
  33. /* _tkinter.c -- Interface to libtk.a and libtcl.a. */
  34.  
  35. /* TCL/TK VERSION INFO:
  36.  
  37.    Unix:
  38.     This should work with any version from Tcl 4.0 / Tck 7.4.
  39.     Do not use older versions.
  40.  
  41.    Mac and Windows:
  42.     Use Tcl 4.1p1 / Tk 7.5p1 or possibly newer.
  43.     It does not seem to work reliably with the original 4.1/7.5
  44.     release.  (4.0/7.4 were never released for these platforms.)
  45. */
  46.  
  47. #include "Python.h"
  48. #include <ctype.h>
  49.  
  50. #ifdef macintosh
  51. #define MAC_TCL
  52. #endif
  53.  
  54. #include <tcl.h>
  55. #include <tk.h>
  56.  
  57. extern char *Py_GetProgramName ();
  58.  
  59. /* Internal declarations from tkInt.h.  */
  60. #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001
  61. extern int Tk_GetNumMainWindows();
  62. #else
  63. extern int tk_NumMainWindows;
  64. #define Tk_GetNumMainWindows() (tk_NumMainWindows)
  65. #define NEED_TKCREATEMAINWINDOW 1
  66. #endif
  67.  
  68. #if TK_MAJOR_VERSION < 4
  69. extern struct { Tk_Window win; } *tkMainWindowList;
  70. #endif
  71.  
  72. #ifdef macintosh
  73.  
  74. /*
  75. ** Additional cruft needed by Tcl/Tk on the Mac.
  76. ** This is for Tcl 7.5 and Tk 4.1 (patch release 1).
  77. */
  78.  
  79. /* ckfree() expects a char* */
  80. #define FREECAST (char *)
  81.  
  82. #include <Events.h> /* For EventRecord */
  83.  
  84. typedef int (*TclMacConvertEventPtr) Py_PROTO((EventRecord *eventPtr));
  85. void TclMacSetEventProc Py_PROTO((TclMacConvertEventPtr procPtr));
  86. int TkMacConvertEvent Py_PROTO((EventRecord *eventPtr));
  87.  
  88. staticforward int PyMacConvertEvent Py_PROTO((EventRecord *eventPtr));
  89.  
  90. #endif /* macintosh */
  91.  
  92. #ifndef FREECAST
  93. #define FREECAST (char *)
  94. #endif
  95.  
  96. /**** Tkapp Object Declaration ****/
  97.  
  98. staticforward PyTypeObject Tkapp_Type;
  99.  
  100. typedef struct
  101.   {
  102.     PyObject_HEAD
  103.     Tcl_Interp *interp;
  104. #ifdef NEED_TKCREATEMAINWINDOW
  105.     Tk_Window tkwin;
  106. #endif
  107.   }
  108. TkappObject;
  109.  
  110. #define Tkapp_Check(v) ((v)->ob_type == &Tkapp_Type)
  111. #ifdef NEED_TKCREATEMAINWINDOW
  112. #define Tkapp_Tkwin(v)  (((TkappObject *) (v))->tkwin)
  113. #endif
  114. #define Tkapp_Interp(v) (((TkappObject *) (v))->interp)
  115. #define Tkapp_Result(v) (((TkappObject *) (v))->interp->result)
  116.  
  117. #define DEBUG_REFCNT(v) (printf ("DEBUG: id=%p, refcnt=%i\n", \
  118.                  (void *) v, ((PyObject *) v)->ob_refcnt))
  119.  
  120. /**** Error Handling ****/
  121.  
  122. static PyObject *Tkinter_TclError;
  123. static int quitMainLoop = 0;
  124. static int errorInCmd = 0;
  125. static PyObject *excInCmd;
  126. static PyObject *valInCmd;
  127. static PyObject *trbInCmd;
  128.  
  129. static PyObject *
  130. Tkinter_Error (v)
  131.      PyObject *v;
  132. {
  133.   PyErr_SetString (Tkinter_TclError, Tkapp_Result (v));
  134.   return NULL;
  135. }
  136.  
  137. int
  138. PythonCmd_Error (interp)
  139.      Tcl_Interp *interp;
  140. {
  141.   errorInCmd = 1;
  142.   PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd);
  143.   return TCL_ERROR;
  144. }
  145.  
  146. /**** Utils ****/
  147.  
  148. static char *
  149. AsString (value, tmp)
  150.      PyObject *value;
  151.      PyObject *tmp;
  152. {
  153.   if (PyString_Check (value))
  154.     return PyString_AsString (value);
  155.   else
  156.     {
  157.       PyObject *v;
  158.  
  159.       v = PyObject_Str (value);
  160.       PyList_Append (tmp, v);
  161.       Py_DECREF (v);
  162.       return PyString_AsString (v);
  163.     }
  164. }
  165.  
  166. #define ARGSZ 64
  167.  
  168. static char *
  169. Merge (args)
  170.      PyObject *args;
  171. {
  172.   PyObject *tmp;
  173.   char *argvStore[ARGSZ];
  174.   char **argv;
  175.   int fvStore[ARGSZ];
  176.   int *fv;
  177.   int argc;
  178.   char *res;
  179.   int i;
  180.  
  181.   tmp = PyList_New (0);
  182.   argv = argvStore;
  183.   fv = fvStore;
  184.  
  185.   if (args == NULL)
  186.     {
  187.       argc = 0;
  188.     }
  189.   else if (!PyTuple_Check (args))
  190.     {
  191.       argc = 1;
  192.       fv[0] = 0;
  193.       argv[0] = AsString (args, tmp);
  194.     }
  195.   else
  196.     {
  197.       PyObject *v;
  198.  
  199.       if (PyTuple_Size (args) > ARGSZ)
  200.     {
  201.       argv = (char **) ckalloc (PyTuple_Size (args) * sizeof (char *));
  202.       fv = (int *) ckalloc (PyTuple_Size (args) * sizeof (int));
  203.       if (argv == NULL || fv == NULL)
  204.         PyErr_NoMemory ();
  205.     }
  206.  
  207.       argc = PyTuple_Size (args);
  208.       for (i = 0; i < argc; i++)
  209.     {
  210.       v = PyTuple_GetItem (args, i);
  211.       if (PyTuple_Check (v))
  212.         {
  213.           fv[i] = 1;
  214.           argv[i] = Merge (v);
  215.         }
  216.       else if (v == Py_None)
  217.         {
  218.           argc = i;
  219.           break;
  220.         }
  221.       else
  222.         {
  223.           fv[i] = 0;
  224.           argv[i] = AsString (v, tmp);
  225.         }
  226.     }
  227.     }
  228.  
  229.   res = Tcl_Merge (argc, argv);
  230.  
  231.   Py_DECREF (tmp);
  232.   for (i = 0; i < argc; i++)
  233.     if (fv[i]) ckfree (argv[i]);
  234.   if (argv != argvStore)
  235.     ckfree (FREECAST argv);
  236.   if (fv != fvStore)
  237.     ckfree (FREECAST fv);
  238.  
  239.   return res;
  240. }
  241.  
  242. static PyObject *
  243. Split (self, list)
  244.      PyObject *self;
  245.      char *list;
  246. {
  247.   int argc;
  248.   char **argv;
  249.   PyObject *v;
  250.  
  251.   if (list == NULL)
  252.     {
  253.       Py_INCREF (Py_None);
  254.       return Py_None;
  255.     }
  256.  
  257.   if (Tcl_SplitList (Tkapp_Interp (self), list, &argc, &argv) == TCL_ERROR)
  258.     return Tkinter_Error (self);
  259.  
  260.   if (argc == 0)
  261.     v = PyString_FromString ("");
  262.   else if (argc == 1)
  263.     v = PyString_FromString (argv[0]);
  264.   else
  265.     {
  266.       int i;
  267.  
  268.       v = PyTuple_New (argc);
  269.       for (i = 0; i < argc; i++)
  270.     PyTuple_SetItem (v, i, Split (self, argv[i]));
  271.     }
  272.  
  273.   ckfree (FREECAST argv);
  274.   return v;
  275. }
  276.  
  277. /**** Tkapp Object ****/
  278.  
  279. #ifndef WITH_APPINIT
  280. int
  281. Tcl_AppInit (interp)
  282.      Tcl_Interp *interp;
  283. {
  284.   Tk_Window main;
  285.  
  286.   main = Tk_MainWindow(interp);
  287.   if (Tcl_Init (interp) == TCL_ERROR) {
  288.     fprintf(stderr, "Tcl_Init error: %s\n", interp->result);
  289.     return TCL_ERROR;
  290.   }
  291.   if (Tk_Init (interp) == TCL_ERROR) {
  292.     fprintf(stderr, "Tk_Init error: %s\n", interp->result);
  293.     return TCL_ERROR;
  294.   }
  295.   return TCL_OK;
  296. }
  297.  
  298. #endif /* !WITH_APPINIT */
  299.  
  300. /* Initialize the Tk application; see the `main' function in
  301.    `tkMain.c'.  */
  302. static TkappObject *
  303. Tkapp_New (screenName, baseName, className, interactive)
  304.      char *screenName;
  305.      char *baseName;
  306.      char *className;
  307.      int interactive;
  308. {
  309.   TkappObject *v;
  310.   char *argv0;
  311.   
  312.   v = PyObject_NEW (TkappObject, &Tkapp_Type);
  313.   if (v == NULL)
  314.     return NULL;
  315.  
  316.   v->interp = Tcl_CreateInterp ();
  317.  
  318. #ifdef NEED_TKCREATEMAINWINDOW
  319.   v->tkwin = Tk_CreateMainWindow (v->interp, screenName, 
  320.                   baseName, className);
  321.   if (v->tkwin == NULL)
  322.     return (TkappObject *) Tkinter_Error ((PyObject *) v);
  323.  
  324.   Tk_GeometryRequest (v->tkwin, 200, 200);
  325. #endif
  326.  
  327.   if (screenName != NULL)
  328.     Tcl_SetVar2 (v->interp, "env", "DISPLAY", screenName, TCL_GLOBAL_ONLY);
  329.  
  330.   if (interactive)
  331.     Tcl_SetVar (v->interp, "tcl_interactive", "1", TCL_GLOBAL_ONLY);
  332.   else
  333.     Tcl_SetVar (v->interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
  334.  
  335.   /* This is used to get the application class for Tk 4.1 and up */
  336.   argv0 = (char*) ckalloc (strlen (className) + 1);
  337.   if (argv0 != NULL) {
  338.     strcpy (argv0, className);
  339.     if (isupper (argv0[0]))
  340.       argv0[0] = tolower (argv0[0]);
  341.     Tcl_SetVar (v->interp, "argv0", argv0, TCL_GLOBAL_ONLY);
  342.     ckfree (argv0);
  343.   }
  344.  
  345.   if (Tcl_AppInit (v->interp) != TCL_OK)
  346.     return (TkappObject *) Tkinter_Error (v);
  347.  
  348.   return v;
  349. }
  350.  
  351. /** Tcl Eval **/
  352.  
  353. static PyObject *
  354. Tkapp_Call (self, args)
  355.      PyObject *self;
  356.      PyObject *args;
  357. {
  358.   char *cmd;
  359.  
  360.   cmd = Merge (args);
  361.   if (Tcl_Eval (Tkapp_Interp (self), cmd) == TCL_ERROR)
  362.     {
  363.       ckfree (cmd);
  364.       return Tkinter_Error (self);
  365.     }
  366.  
  367.   ckfree (cmd);
  368.   return PyString_FromString (Tkapp_Result (self));
  369. }
  370.  
  371. static PyObject *
  372. Tkapp_GlobalCall (self, args)
  373.      PyObject *self;
  374.      PyObject *args;
  375. {
  376.   char *cmd;
  377.  
  378.   cmd = Merge (args);
  379.   if (Tcl_GlobalEval (Tkapp_Interp (self), cmd) == TCL_ERROR)
  380.     {
  381.       ckfree (cmd);
  382.       return Tkinter_Error (self);
  383.     }
  384.   
  385.   ckfree (cmd);
  386.   return PyString_FromString (Tkapp_Result (self));
  387. }
  388.  
  389. static PyObject *
  390. Tkapp_Eval (self, args)
  391.      PyObject *self;
  392.      PyObject *args;
  393. {
  394.   char *script;
  395.   
  396.   if (!PyArg_Parse (args, "s", &script))
  397.     return NULL;
  398.  
  399.   if (Tcl_Eval (Tkapp_Interp (self), script) == TCL_ERROR)
  400.     return Tkinter_Error (self);
  401.   
  402.   return PyString_FromString (Tkapp_Result (self));
  403. }
  404.  
  405. static PyObject *
  406. Tkapp_GlobalEval (self, args)
  407.      PyObject *self;
  408.      PyObject *args;
  409. {
  410.   char *script;
  411.   
  412.   if (!PyArg_Parse (args, "s", &script))
  413.     return NULL;
  414.  
  415.   if (Tcl_GlobalEval (Tkapp_Interp (self), script) == TCL_ERROR)
  416.     return Tkinter_Error (self);
  417.  
  418.   return PyString_FromString (Tkapp_Result (self));
  419. }
  420.  
  421. static PyObject *
  422. Tkapp_EvalFile (self, args)
  423.      PyObject *self;
  424.      PyObject *args;
  425. {
  426.   char *fileName;
  427.  
  428.   if (!PyArg_Parse (args, "s", &fileName))
  429.     return NULL;
  430.  
  431.   if (Tcl_EvalFile (Tkapp_Interp (self), fileName) == TCL_ERROR)
  432.     return Tkinter_Error (self);
  433.  
  434.   return PyString_FromString (Tkapp_Result (self));
  435. }
  436.  
  437. static PyObject *
  438. Tkapp_Record (self, args)
  439.      PyObject *self;
  440.      PyObject *args;
  441. {
  442.   char *script;
  443.  
  444.   if (!PyArg_Parse (args, "s", &script))
  445.     return NULL;
  446.  
  447.   if (Tcl_RecordAndEval (Tkapp_Interp (self), 
  448.              script, TCL_NO_EVAL) == TCL_ERROR)
  449.     return Tkinter_Error (self);
  450.  
  451.   return PyString_FromString (Tkapp_Result (self));
  452. }
  453.  
  454. static PyObject *
  455. Tkapp_AddErrorInfo (self, args)
  456.      PyObject *self;
  457.      PyObject *args;
  458. {
  459.   char *msg;
  460.  
  461.   if (!PyArg_Parse (args, "s", &msg))
  462.     return NULL;
  463.   Tcl_AddErrorInfo (Tkapp_Interp (self), msg);
  464.  
  465.   Py_INCREF(Py_None);
  466.   return Py_None;
  467. }
  468.  
  469. /** Tcl Variable **/
  470.  
  471. static PyObject *
  472. SetVar (self, args, flags)
  473.      PyObject *self;
  474.      PyObject *args;
  475.      int flags;
  476. {
  477.   char *name1, *name2, *ok;
  478.   PyObject *newValue;
  479.   PyObject *tmp;
  480.  
  481.   tmp = PyList_New (0);
  482.  
  483.   if (PyArg_Parse (args, "(sO)", &name1, &newValue))
  484.     ok = Tcl_SetVar (Tkapp_Interp (self), name1, 
  485.              AsString (newValue, tmp), flags); /* XXX Merge? */
  486.   else if (PyArg_Parse (args, "(ssO)", &name1, &name2, &newValue))
  487.     ok = Tcl_SetVar2 (Tkapp_Interp (self), name1, name2, 
  488.               AsString (newValue, tmp), flags);
  489.   else
  490.     {
  491.       Py_DECREF (tmp);
  492.       return NULL;
  493.     }
  494.   Py_DECREF (tmp);
  495.  
  496.   if (!ok)
  497.     return Tkinter_Error (self);
  498.  
  499.   Py_INCREF (Py_None);
  500.   return Py_None;
  501. }
  502.  
  503. static PyObject *
  504. Tkapp_SetVar (self, args)
  505.      PyObject *self;
  506.      PyObject *args;
  507. {
  508.   return SetVar (self, args, TCL_LEAVE_ERR_MSG);
  509. }
  510.  
  511. static PyObject *
  512. Tkapp_GlobalSetVar (self, args)
  513.      PyObject *self;
  514.      PyObject *args;
  515. {
  516.   return SetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
  517. }
  518.  
  519. static PyObject *
  520. GetVar (self, args, flags)
  521.      PyObject *self;
  522.      PyObject *args;
  523.      int flags;
  524. {
  525.   char *name1, *name2, *s;
  526.  
  527.   if (PyArg_Parse (args, "s", &name1))
  528.     s = Tcl_GetVar (Tkapp_Interp (self), name1, flags);
  529.   else if (PyArg_Parse (args, "(ss)", &name1, &name2))
  530.     s = Tcl_GetVar2 (Tkapp_Interp (self), name1, name2, flags);
  531.   else
  532.     return NULL;
  533.  
  534.   if (s == NULL)
  535.     return Tkinter_Error (self);
  536.  
  537.   return PyString_FromString (s);
  538. }
  539.  
  540. static PyObject *
  541. Tkapp_GetVar (self, args)
  542.      PyObject *self;
  543.      PyObject *args;
  544. {
  545.   return GetVar (self, args, TCL_LEAVE_ERR_MSG);
  546. }
  547.  
  548. static PyObject *
  549. Tkapp_GlobalGetVar (self, args)
  550.      PyObject *self;
  551.      PyObject *args;
  552. {
  553.   return GetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
  554. }
  555.  
  556. static PyObject *
  557. UnsetVar (self, args, flags)
  558.      PyObject *self;
  559.      PyObject *args;
  560.      int flags;
  561. {
  562.   char *name1, *name2;
  563.   int code;
  564.  
  565.   if (PyArg_Parse (args, "s", &name1))
  566.     code = Tcl_UnsetVar (Tkapp_Interp (self), name1, flags);
  567.   else if (PyArg_Parse (args, "(ss)", &name1, &name2))
  568.     code = Tcl_UnsetVar2 (Tkapp_Interp (self), name1, name2, flags);
  569.   else
  570.     return NULL;
  571.  
  572.   if (code == TCL_ERROR)
  573.     return Tkinter_Error (self);
  574.  
  575.   Py_INCREF (Py_None);
  576.   return Py_None;
  577. }
  578.  
  579. static PyObject *
  580. Tkapp_UnsetVar (self, args)
  581.      PyObject *self;
  582.      PyObject *args;
  583. {
  584.   return UnsetVar (self, args, TCL_LEAVE_ERR_MSG);
  585. }
  586.  
  587. static PyObject *
  588. Tkapp_GlobalUnsetVar (self, args)
  589.      PyObject *self;
  590.      PyObject *args;
  591. {
  592.   return UnsetVar (self, args, TCL_LEAVE_ERR_MSG | TCL_GLOBAL_ONLY);
  593. }
  594.  
  595. /** Tcl to Python **/
  596.  
  597. static PyObject *
  598. Tkapp_GetInt (self, args)
  599.      PyObject *self;
  600.      PyObject *args;
  601. {
  602.   char *s;
  603.   int v;
  604.  
  605.   if (!PyArg_Parse (args, "s", &s))
  606.     return NULL;
  607.   if (Tcl_GetInt (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  608.     return Tkinter_Error (self);
  609.   return Py_BuildValue ("i", v);
  610. }
  611.  
  612. static PyObject *
  613. Tkapp_GetDouble (self, args)
  614.      PyObject *self;
  615.      PyObject *args;
  616. {
  617.   char *s;
  618.   double v;
  619.  
  620.   if (!PyArg_Parse (args, "s", &s))
  621.     return NULL;
  622.   if (Tcl_GetDouble (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  623.     return Tkinter_Error (self);
  624.   return Py_BuildValue ("d", v);
  625. }
  626.  
  627. static PyObject *
  628. Tkapp_GetBoolean (self, args)
  629.      PyObject *self;
  630.      PyObject *args;
  631. {
  632.   char *s;
  633.   int v;
  634.  
  635.   if (!PyArg_Parse (args, "s", &s))
  636.     return NULL;
  637.   if (Tcl_GetBoolean (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  638.     return Tkinter_Error (self);
  639.   return Py_BuildValue ("i", v);
  640. }
  641.  
  642. static PyObject *
  643. Tkapp_ExprString (self, args)
  644.      PyObject *self;
  645.      PyObject *args;
  646. {
  647.   char *s;
  648.  
  649.   if (!PyArg_Parse (args, "s", &s))
  650.     return NULL;
  651.   if (Tcl_ExprString (Tkapp_Interp (self), s) == TCL_ERROR)
  652.     return Tkinter_Error (self);
  653.   return Py_BuildValue ("s", Tkapp_Result (self));
  654. }
  655.  
  656. static PyObject *
  657. Tkapp_ExprLong (self, args)
  658.      PyObject *self;
  659.      PyObject *args;
  660. {
  661.   char *s;
  662.   long v;
  663.  
  664.   if (!PyArg_Parse (args, "s", &s))
  665.     return NULL;
  666.   if (Tcl_ExprLong (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  667.     return Tkinter_Error (self);
  668.   return Py_BuildValue ("l", v);
  669. }
  670.  
  671. static PyObject *
  672. Tkapp_ExprDouble (self, args)
  673.      PyObject *self;
  674.      PyObject *args;
  675. {
  676.   char *s;
  677.   double v;
  678.  
  679.   if (!PyArg_Parse (args, "s", &s))
  680.     return NULL;
  681.   if (Tcl_ExprDouble (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  682.     return Tkinter_Error (self);
  683.   return Py_BuildValue ("d", v);
  684. }
  685.  
  686. static PyObject *
  687. Tkapp_ExprBoolean (self, args)
  688.      PyObject *self;
  689.      PyObject *args;
  690. {
  691.   char *s;
  692.   int v;
  693.  
  694.   if (!PyArg_Parse (args, "s", &s))
  695.     return NULL;
  696.   if (Tcl_ExprBoolean (Tkapp_Interp (self), s, &v) == TCL_ERROR)
  697.     return Tkinter_Error (self);
  698.   return Py_BuildValue ("i", v);
  699. }
  700.  
  701. static PyObject *
  702. Tkapp_SplitList (self, args)
  703.      PyObject *self;
  704.      PyObject *args;
  705. {
  706.   char *list;
  707.   int argc;
  708.   char **argv;
  709.   PyObject *v;
  710.   int i;
  711.  
  712.   if (!PyArg_Parse (args, "s", &list))
  713.     return NULL;
  714.  
  715.   if (Tcl_SplitList (Tkapp_Interp (self), list, &argc, &argv) == TCL_ERROR)
  716.     return Tkinter_Error (self);
  717.  
  718.   v = PyTuple_New (argc);
  719.   for (i = 0; i < argc; i++)
  720.     PyTuple_SetItem (v, i, PyString_FromString (argv[i]));
  721.  
  722.   ckfree (FREECAST argv);
  723.   return v;
  724. }
  725.  
  726. static PyObject *
  727. Tkapp_Split (self, args)
  728.      PyObject *self;
  729.      PyObject *args;
  730. {
  731.   char *list;
  732.  
  733.   if (!PyArg_Parse (args, "s", &list))
  734.     return NULL;
  735.   return Split (self, list);
  736. }
  737.  
  738. static PyObject *
  739. Tkapp_Merge (self, args)
  740.      PyObject *self;
  741.      PyObject *args;
  742. {
  743.   char *s;
  744.   PyObject *v;
  745.  
  746.   s = Merge (args);
  747.   v = PyString_FromString (s);
  748.   ckfree (s);
  749.   return v;
  750. }
  751.  
  752. /** Tcl Command **/
  753.  
  754. /* This is the Tcl command that acts as a wrapper for Python
  755.    function or method.  */
  756. static int
  757. PythonCmd (clientData, interp, argc, argv)
  758.      ClientData clientData;    /* Is (self, func) */
  759.      Tcl_Interp *interp;
  760.      int argc;
  761.      char *argv[];
  762. {
  763.   PyObject *self, *func, *arg, *res, *tmp;
  764.   int i;
  765.  
  766.   self = PyTuple_GetItem ((PyObject *) clientData, 0);
  767.   func = PyTuple_GetItem ((PyObject *) clientData, 1);
  768.  
  769.   /* Create argument list (argv1, ..., argvN) */
  770.   arg = PyTuple_New (argc - 1);
  771.   for (i = 0; i < (argc - 1); i++)
  772.     PyTuple_SetItem (arg, i, PyString_FromString (argv[i + 1]));
  773.   
  774.   res = PyEval_CallObject (func, arg);
  775.   Py_DECREF (arg);
  776.  
  777.   if (res == NULL)
  778.     return PythonCmd_Error (interp);
  779.  
  780.   tmp = PyList_New (0);
  781.   Tcl_SetResult (Tkapp_Interp (self), AsString (res, tmp), TCL_VOLATILE);
  782.   Py_DECREF (res);
  783.   Py_DECREF (tmp);
  784.  
  785.   return TCL_OK;
  786. }
  787.  
  788. static void
  789. PythonCmdDelete (clientData)
  790.      ClientData clientData;    /* Is (self, func) */
  791. {
  792.   Py_DECREF ((PyObject *) clientData);
  793. }
  794.  
  795. static PyObject *
  796. Tkapp_CreateCommand (self, args)
  797.      PyObject *self;
  798.      PyObject *args;
  799. {
  800.   char *cmdName;
  801.   PyObject *data;
  802.   PyObject *func;
  803.   
  804.   /* Args is: (cmdName, func) */
  805.   if (!PyTuple_Check (args) 
  806.       || !(PyTuple_Size (args) == 2)
  807.       || !PyString_Check (PyTuple_GetItem (args, 0))
  808.       || !PyCallable_Check (PyTuple_GetItem (args, 1)))
  809.     {
  810.       PyErr_SetString (PyExc_TypeError, "bad argument list");
  811.       return NULL;
  812.     }
  813.  
  814.   cmdName = PyString_AsString (PyTuple_GetItem (args, 0));
  815.   func = PyTuple_GetItem (args, 1);
  816.  
  817.   data = PyTuple_New (2);   /* ClientData is: (self, func) */
  818.  
  819.   Py_INCREF (self);
  820.   PyTuple_SetItem (data, 0, self);
  821.  
  822.   Py_INCREF (func);
  823.   PyTuple_SetItem (data, 1, func);
  824.  
  825.   Tcl_CreateCommand (Tkapp_Interp (self), cmdName, PythonCmd,
  826.              (ClientData) data, PythonCmdDelete);
  827.  
  828.   Py_INCREF (Py_None);
  829.   return Py_None;
  830. }
  831.  
  832. static PyObject *
  833. Tkapp_DeleteCommand (self, args)
  834.      PyObject *self;
  835.      PyObject *args;
  836. {
  837.   char *cmdName;
  838.  
  839.   if (!PyArg_Parse (args, "s", &cmdName))
  840.     return NULL;
  841.   if (Tcl_DeleteCommand (Tkapp_Interp (self), cmdName) == -1)
  842.     {
  843.       PyErr_SetString (Tkinter_TclError, "can't delete Tcl command");
  844.       return NULL;
  845.     }
  846.   Py_INCREF (Py_None);
  847.   return Py_None;
  848. }
  849.  
  850. /** File Handler **/
  851.  
  852. static void
  853. FileHandler (clientData, mask)
  854.      ClientData clientData;    /* Is: (func, file) */
  855.      int mask;
  856. {
  857.   PyObject *func, *file, *arg, *res;
  858.  
  859.   func = PyTuple_GetItem ((PyObject *) clientData, 0);
  860.   file = PyTuple_GetItem ((PyObject *) clientData, 1);
  861.  
  862.   arg = Py_BuildValue ("(Oi)", file, (long) mask);
  863.   res = PyEval_CallObject (func, arg);
  864.   Py_DECREF (arg);
  865.   if (res == NULL)
  866.     {
  867.       errorInCmd = 1;
  868.       PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd);
  869.     }
  870.   Py_XDECREF (res);
  871. }
  872.  
  873. static int
  874. GetFileNo (file)
  875.     PyObject *file; /* Either an int >= 0 or an object with a
  876.                .fileno() method that returns an int >= 0 */
  877. {
  878.     PyObject *meth, *args, *res;
  879.     int id;
  880.     if (PyInt_Check(file)) {
  881.         id = PyInt_AsLong(file);
  882.         if (id < 0)
  883.             PyErr_SetString(PyExc_ValueError, "invalid file id");
  884.         return id;
  885.     }
  886.     meth = PyObject_GetAttrString(file, "fileno");
  887.     if (meth == NULL)
  888.         return -1;
  889.     args = PyTuple_New(0);
  890.     if (args == NULL)
  891.         return -1;
  892.     res = PyEval_CallObject(meth, args);
  893.     Py_DECREF(args);
  894.     Py_DECREF(meth);
  895.     if (res == NULL)
  896.         return -1;
  897.     if (PyInt_Check(res))
  898.         id = PyInt_AsLong(res);
  899.     else
  900.         id = -1;
  901.     if (id < 0)
  902.         PyErr_SetString(PyExc_ValueError,
  903.                 "invalid fileno() return value");
  904.     Py_DECREF(res);
  905.     return id;
  906. }
  907.  
  908. static PyObject *
  909. Tkapp_CreateFileHandler (self, args)
  910.      PyObject *self;
  911.      PyObject *args;        /* Is (file, mask, func) */
  912. {
  913.   PyObject *file, *func, *data;
  914.   int mask, id;
  915. #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001
  916.   Tcl_File tfile;
  917. #endif
  918.  
  919.   if (!PyArg_Parse (args, "(OiO)", &file, &mask, &func))
  920.     return NULL;
  921.   id = GetFileNo (file);
  922.   if (id < 0)
  923.     return NULL;
  924.   if (!PyCallable_Check(func))
  925.     {
  926.       PyErr_SetString (PyExc_TypeError, "bad argument list");
  927.       return NULL;
  928.     }
  929.  
  930.   /* ClientData is: (func, file) */
  931.   data = Py_BuildValue ("(OO)", func, file);
  932.  
  933. #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001
  934. #ifdef MS_WINDOWS
  935.   /* We assume this is a socket... */
  936.   tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET);
  937. #else
  938.   tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD);
  939. #endif
  940.   /* Oughtta check for null Tcl_File object... */
  941.   Tcl_CreateFileHandler (tfile, mask, FileHandler, (ClientData) data);
  942. #else
  943.   Tk_CreateFileHandler (id, mask, FileHandler, (ClientData) data);
  944. #endif
  945.   /* XXX fileHandlerDict */
  946.  
  947.   Py_INCREF (Py_None);
  948.   return Py_None;
  949. }
  950.  
  951. static PyObject *
  952. Tkapp_DeleteFileHandler (self, args)
  953.      PyObject *self;
  954.      PyObject *args;        /* Args: file */
  955. {
  956.   PyObject *file;
  957.   int id;
  958. #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001
  959.   Tcl_File tfile;
  960. #endif
  961.   
  962.   if (!PyArg_Parse (args, "O", &file))
  963.     return NULL;
  964.   id = GetFileNo (file);
  965.   if (id < 0)
  966.     return NULL;
  967.  
  968. #if (TK_MAJOR_VERSION*1000 + TK_MINOR_VERSION) >= 4001
  969. #ifdef MS_WINDOWS
  970.   /* We assume this is a socket... */
  971.   tfile = Tcl_GetFile((ClientData)id, TCL_WIN_SOCKET);
  972. #else
  973.   tfile = Tcl_GetFile((ClientData)id, TCL_UNIX_FD);
  974. #endif
  975.   /* Oughtta check for null Tcl_File object... */
  976.   Tcl_DeleteFileHandler(tfile);
  977. #else
  978.   Tk_DeleteFileHandler (id);
  979. #endif
  980.   /* XXX fileHandlerDict */
  981.   Py_INCREF (Py_None);
  982.   return Py_None;
  983. }
  984.  
  985. /**** Tktt Object (timer token) ****/
  986.  
  987. staticforward PyTypeObject Tktt_Type;
  988.  
  989. typedef struct
  990.   {
  991.     PyObject_HEAD
  992.     Tk_TimerToken token;
  993.     PyObject *func;
  994.   }
  995. TkttObject;
  996.  
  997. static PyObject *
  998. Tktt_DeleteTimerHandler (self, args)
  999.      PyObject *self;
  1000.      PyObject *args;
  1001. {
  1002.   TkttObject *v = (TkttObject *) self;
  1003.  
  1004.   if (!PyArg_Parse (args, ""))
  1005.     return NULL;
  1006.   if (v->func != NULL)
  1007.     {
  1008.       Tk_DeleteTimerHandler (v->token);
  1009.       PyMem_DEL (v->func);
  1010.       v->func = NULL;
  1011.     }
  1012.   Py_INCREF (Py_None);
  1013.   return Py_None;
  1014. }
  1015.  
  1016. static PyMethodDef Tktt_methods[] =
  1017. {
  1018.   {"deletetimerhandler", Tktt_DeleteTimerHandler},
  1019.   {NULL, NULL}
  1020. };
  1021.  
  1022. static TkttObject *
  1023. Tktt_New (token, func)
  1024.      Tk_TimerToken token;
  1025.      PyObject *func;
  1026. {
  1027.   TkttObject *v;
  1028.   
  1029.   v = PyObject_NEW (TkttObject, &Tktt_Type);
  1030.   if (v == NULL)
  1031.     return NULL;
  1032.  
  1033.   v->token = token;
  1034.   v->func = func;
  1035.   Py_INCREF (v->func);
  1036.   return v;
  1037. }
  1038.  
  1039. static void
  1040. Tktt_Dealloc (self)
  1041.      PyObject *self;
  1042. {
  1043.   PyMem_DEL (self);
  1044. }
  1045.  
  1046. static int
  1047. Tktt_Print (self, fp, flags)
  1048.      PyObject *self;
  1049.      FILE *fp;
  1050.      int flags;
  1051. {
  1052.   TkttObject *v = (TkttObject *) self;
  1053.  
  1054.   fprintf(fp, "<tktimertoken at 0x%x%s>", v,
  1055.     v->func == NULL ? ", handler deleted" : "");
  1056.   return 0;
  1057. }
  1058.  
  1059. static PyObject *
  1060. Tktt_GetAttr (self, name)
  1061.      PyObject *self;
  1062.      char *name;
  1063. {
  1064.   return Py_FindMethod (Tktt_methods, self, name);
  1065. }
  1066.  
  1067. static PyTypeObject Tktt_Type =
  1068. {
  1069.   PyObject_HEAD_INIT (NULL)
  1070.   0,                /*ob_size */
  1071.   "tktimertoken",        /*tp_name */
  1072.   sizeof (TkttObject),        /*tp_basicsize */
  1073.   0,                /*tp_itemsize */
  1074.   Tktt_Dealloc,            /*tp_dealloc */
  1075.   Tktt_Print,            /*tp_print */
  1076.   Tktt_GetAttr,            /*tp_getattr */
  1077.   0,                /*tp_setattr */
  1078.   0,                /*tp_compare */
  1079.   0,                /*tp_repr */
  1080.   0,                /*tp_as_number */
  1081.   0,                /*tp_as_sequence */
  1082.   0,                /*tp_as_mapping */
  1083.   0,                /*tp_hash */
  1084. };
  1085.  
  1086. /** Timer Handler **/
  1087.  
  1088. static void
  1089. TimerHandler (clientData)
  1090.      ClientData clientData;
  1091. {
  1092.   PyObject *func = (PyObject *) clientData;
  1093.   PyObject *arg, *res;
  1094.  
  1095.   arg = PyTuple_New (0);
  1096.   res = PyEval_CallObject (func, arg);
  1097.   Py_DECREF (arg);
  1098.   if (res == NULL)
  1099.     {
  1100.       errorInCmd = 1;
  1101.       PyErr_Fetch (&excInCmd, &valInCmd, &trbInCmd);
  1102.     }
  1103.   else
  1104.     Py_DECREF (res);
  1105. }
  1106.  
  1107. static PyObject *
  1108. Tkapp_CreateTimerHandler (self, args)
  1109.      PyObject *self;
  1110.      PyObject *args;        /* Is (milliseconds, func) */
  1111. {
  1112.   int milliseconds;
  1113.   PyObject *func;
  1114.   Tk_TimerToken token;
  1115.  
  1116.   if (!PyArg_Parse (args, "(iO)", &milliseconds, &func))
  1117.     return NULL;
  1118.   if (!PyCallable_Check(func))
  1119.     {
  1120.       PyErr_SetString (PyExc_TypeError, "bad argument list");
  1121.       return NULL;
  1122.     }
  1123.   token = Tk_CreateTimerHandler(milliseconds, TimerHandler, (ClientData) func);
  1124.   return (PyObject *) Tktt_New (token, func);
  1125. }
  1126.  
  1127. /** Event Loop **/
  1128.  
  1129. static PyObject *
  1130. Tkapp_MainLoop (self, args)
  1131.      PyObject *self;
  1132.      PyObject *args;
  1133. {
  1134.   int threshold = 0;
  1135.  
  1136.   if (!PyArg_ParseTuple (args, "|i", &threshold))
  1137.     return NULL;
  1138.  
  1139.   quitMainLoop = 0;
  1140.   while (Tk_GetNumMainWindows() > threshold && !quitMainLoop && !errorInCmd)
  1141.     {
  1142.       if (PyOS_InterruptOccurred ())
  1143.     {
  1144.       PyErr_SetNone (PyExc_KeyboardInterrupt);
  1145.       return NULL;
  1146.     }
  1147.       Tk_DoOneEvent (0);
  1148.     }
  1149.   quitMainLoop = 0;
  1150.  
  1151.   if (errorInCmd)
  1152.     {
  1153.       errorInCmd = 0;
  1154.       PyErr_Restore (excInCmd, valInCmd, trbInCmd);
  1155.       excInCmd = valInCmd = trbInCmd = NULL;
  1156.       return NULL;
  1157.     }
  1158.   Py_INCREF (Py_None);
  1159.   return Py_None;
  1160. }
  1161.  
  1162. static PyObject *
  1163. Tkapp_DoOneEvent (self, args)
  1164.     PyObject *self;
  1165.     PyObject *args;
  1166. {
  1167.     int    flags = TK_ALL_EVENTS;
  1168.     int rv;
  1169.  
  1170.     if (!PyArg_ParseTuple (args, "|i", &flags))
  1171.       return NULL;
  1172.     rv = Tk_DoOneEvent(flags);
  1173.     return Py_BuildValue ("i", rv);
  1174. }
  1175.  
  1176. static PyObject *
  1177. Tkapp_Quit (self, args)
  1178.      PyObject *self;
  1179.      PyObject *args;
  1180. {
  1181.  
  1182.   if (!PyArg_Parse (args, ""))
  1183.     return NULL;
  1184.   quitMainLoop = 1;
  1185.   Py_INCREF (Py_None);
  1186.   return Py_None;
  1187. }
  1188.  
  1189. /**** Tkapp Method List ****/
  1190.  
  1191. static PyMethodDef Tkapp_methods[] =
  1192. {
  1193.   {"call", Tkapp_Call},
  1194.   {"globalcall", Tkapp_GlobalCall},
  1195.   {"eval", Tkapp_Eval},
  1196.   {"globaleval", Tkapp_GlobalEval},
  1197.   {"evalfile", Tkapp_EvalFile},
  1198.   {"record", Tkapp_Record},
  1199.   {"adderrorinfo", Tkapp_AddErrorInfo},
  1200.   {"setvar", Tkapp_SetVar},
  1201.   {"globalsetvar", Tkapp_GlobalSetVar},
  1202.   {"getvar", Tkapp_GetVar},
  1203.   {"globalgetvar", Tkapp_GlobalGetVar},
  1204.   {"unsetvar", Tkapp_UnsetVar},
  1205.   {"globalunsetvar", Tkapp_GlobalUnsetVar},
  1206.   {"getint", Tkapp_GetInt},
  1207.   {"getdouble", Tkapp_GetDouble},
  1208.   {"getboolean", Tkapp_GetBoolean},
  1209.   {"exprstring", Tkapp_ExprString},
  1210.   {"exprlong", Tkapp_ExprLong},
  1211.   {"exprdouble", Tkapp_ExprDouble},
  1212.   {"exprboolean", Tkapp_ExprBoolean},
  1213.   {"splitlist", Tkapp_SplitList},
  1214.   {"split", Tkapp_Split},
  1215.   {"merge", Tkapp_Merge},
  1216.   {"createcommand", Tkapp_CreateCommand},
  1217.   {"deletecommand", Tkapp_DeleteCommand},
  1218.   {"createfilehandler", Tkapp_CreateFileHandler},
  1219.   {"deletefilehandler", Tkapp_DeleteFileHandler},
  1220.   {"createtimerhandler", Tkapp_CreateTimerHandler},
  1221.   {"mainloop", Tkapp_MainLoop, 1},
  1222.   {"dooneevent", Tkapp_DoOneEvent, 1},
  1223.   {"quit", Tkapp_Quit},
  1224.   {NULL, NULL}
  1225. };
  1226.  
  1227. /**** Tkapp Type Methods ****/
  1228.  
  1229. static void
  1230. Tkapp_Dealloc (self)
  1231.      PyObject *self;
  1232. {
  1233. #ifdef NEED_TKCREATEMAINWINDOW
  1234.   Tk_DestroyWindow (Tkapp_Tkwin (self));
  1235. #endif
  1236.   Tcl_DeleteInterp (Tkapp_Interp (self));
  1237.   PyMem_DEL (self);
  1238. }
  1239.  
  1240. static PyObject *
  1241. Tkapp_GetAttr (self, name)
  1242.      PyObject *self;
  1243.      char *name;
  1244. {
  1245.   return Py_FindMethod (Tkapp_methods, self, name);
  1246. }
  1247.  
  1248. static PyTypeObject Tkapp_Type =
  1249. {
  1250.   PyObject_HEAD_INIT (NULL)
  1251.   0,                /*ob_size */
  1252.   "tkapp",            /*tp_name */
  1253.   sizeof (TkappObject),        /*tp_basicsize */
  1254.   0,                /*tp_itemsize */
  1255.   Tkapp_Dealloc,        /*tp_dealloc */
  1256.   0,                /*tp_print */
  1257.   Tkapp_GetAttr,        /*tp_getattr */
  1258.   0,                /*tp_setattr */
  1259.   0,                /*tp_compare */
  1260.   0,                /*tp_repr */
  1261.   0,                /*tp_as_number */
  1262.   0,                /*tp_as_sequence */
  1263.   0,                /*tp_as_mapping */
  1264.   0,                /*tp_hash */
  1265. };
  1266.  
  1267. /**** Tkinter Module ****/
  1268.  
  1269. static PyObject *
  1270. Tkinter_Create (self, args)
  1271.      PyObject *self;
  1272.      PyObject *args;
  1273. {
  1274.   char *screenName = NULL;
  1275.   char *baseName = NULL;
  1276.   char *className = NULL;
  1277.   int interactive = 0;
  1278.  
  1279.   baseName = strrchr (Py_GetProgramName (), '/');
  1280.   if (baseName != NULL)
  1281.     baseName++;
  1282.   else
  1283.     baseName = Py_GetProgramName ();
  1284.   className = "Tk";
  1285.   
  1286.   if (!PyArg_ParseTuple (args, "|zssi",
  1287.              &screenName, &baseName, &className, &interactive))
  1288.     return NULL;
  1289.  
  1290.   return (PyObject *) Tkapp_New (screenName, baseName, className, 
  1291.                  interactive);
  1292. }
  1293.  
  1294. static PyMethodDef moduleMethods[] =
  1295. {
  1296.   {"create", Tkinter_Create, 1},
  1297.   {"createfilehandler", Tkapp_CreateFileHandler, 0},
  1298.   {"deletefilehandler", Tkapp_DeleteFileHandler, 0},
  1299.   {"createtimerhandler", Tkapp_CreateTimerHandler, 0},
  1300.   {"mainloop", Tkapp_MainLoop, 1},
  1301.   {"dooneevent", Tkapp_DoOneEvent, 1},
  1302.   {"quit", Tkapp_Quit},
  1303.   {NULL, NULL}
  1304. };
  1305.  
  1306. #undef WITH_READLINE /* XXX */
  1307. #ifdef WITH_READLINE
  1308. static int
  1309. EventHook ()
  1310. {
  1311.   if (errorInCmd)        /* XXX Reset tty */
  1312.     {
  1313.       errorInCmd = 0;
  1314.       PyErr_Restore (excInCmd, valInCmd, trbInCmd);
  1315.       excInCmd = valInCmd = trbInCmd = NULL;
  1316.       PyErr_Print ();
  1317.      }
  1318.   if (Tk_GetNumMainWindows() > 0)
  1319.     Tk_DoOneEvent (TK_DONT_WAIT);
  1320.   return 0;
  1321. }
  1322. #endif /* WITH_READLINE */
  1323.  
  1324. static void
  1325. Tkinter_Cleanup ()
  1326. {
  1327. /* This segfault with Tk 4.0 beta and seems unnecessary there as well */
  1328. #if TK_MAJOR_VERSION < 4
  1329.   /* XXX rl_deprep_terminal is static, damned! */
  1330.   while (tkMainWindowList != 0)
  1331.     Tk_DestroyWindow (tkMainWindowList->win);
  1332. #endif
  1333. }
  1334.  
  1335. void
  1336. init_tkinter ()
  1337. {
  1338.   static inited = 0;
  1339.  
  1340. #ifdef WITH_READLINE
  1341.   extern int (*rl_event_hook) ();
  1342. #endif /* WITH_READLINE */
  1343.   PyObject *m, *d, *v;
  1344.  
  1345.   Tkapp_Type.ob_type = &PyType_Type;
  1346.   Tktt_Type.ob_type = &PyType_Type;
  1347.  
  1348.   m = Py_InitModule ("_tkinter", moduleMethods);
  1349.  
  1350.   d = PyModule_GetDict (m);
  1351.   Tkinter_TclError = Py_BuildValue ("s", "TclError");
  1352.   PyDict_SetItemString (d, "TclError", Tkinter_TclError);
  1353.  
  1354.   v = Py_BuildValue ("i", TK_READABLE);
  1355.   PyDict_SetItemString (d, "READABLE", v);
  1356.   v = Py_BuildValue ("i", TK_WRITABLE);
  1357.   PyDict_SetItemString (d, "WRITABLE", v);
  1358.   v = Py_BuildValue ("i", TK_EXCEPTION);
  1359.   PyDict_SetItemString (d, "EXCEPTION", v);
  1360.   v = Py_BuildValue ("i", TK_X_EVENTS);
  1361.   PyDict_SetItemString (d, "X_EVENTS", v);
  1362.   v = Py_BuildValue ("i", TK_FILE_EVENTS);
  1363.   PyDict_SetItemString (d, "FILE_EVENTS", v);
  1364.   v = Py_BuildValue ("i", TK_TIMER_EVENTS);
  1365.   PyDict_SetItemString (d, "TIMER_EVENTS", v);
  1366.   v = Py_BuildValue ("i", TK_IDLE_EVENTS);
  1367.   PyDict_SetItemString (d, "IDLE_EVENTS", v);
  1368.   v = Py_BuildValue ("i", TK_ALL_EVENTS);
  1369.   PyDict_SetItemString (d, "ALL_EVENTS", v);
  1370.   v = Py_BuildValue ("i", TK_DONT_WAIT);
  1371.   PyDict_SetItemString (d, "DONT_WAIT", v);
  1372.   v = Py_BuildValue ("s", TK_VERSION);
  1373.   PyDict_SetItemString (d, "TK_VERSION", v);
  1374.   v = Py_BuildValue ("s", TCL_VERSION);
  1375.   PyDict_SetItemString (d, "TCL_VERSION", v);
  1376.  
  1377. #ifdef WITH_READLINE
  1378.   rl_event_hook = EventHook;
  1379. #endif /* WITH_READLINE */
  1380.  
  1381.   if (!inited)
  1382.     {
  1383.       inited = 1;
  1384.       if (Py_AtExit (Tkinter_Cleanup) != 0)
  1385.     fprintf(stderr,
  1386.         "Tkinter: warning: cleanup procedure not registered\n");
  1387.     }
  1388.  
  1389.   if (PyErr_Occurred ())
  1390.     Py_FatalError ("can't initialize module _tkinter");
  1391. #ifdef macintosh
  1392.   TclMacSetEventProc(PyMacConvertEvent);
  1393. #if GENERATINGCFM
  1394.   mac_addlibresources();
  1395. #endif /* GENERATINGCFM */
  1396. #endif /* macintosh */
  1397. }
  1398.  
  1399.  
  1400. #ifdef macintosh
  1401.  
  1402. /*
  1403. ** Anyone who embeds Tcl/Tk on the Mac must define panic().
  1404. */
  1405.  
  1406. void
  1407. panic(char * format, ...)
  1408. {
  1409.     va_list varg;
  1410.     
  1411.     va_start(varg, format);
  1412.     
  1413.     vfprintf(stderr, format, varg);
  1414.     (void) fflush(stderr);
  1415.     
  1416.     va_end(varg);
  1417.  
  1418.     Py_FatalError("Tcl/Tk panic");
  1419. }
  1420.  
  1421. /*
  1422. ** Pass events to SIOUX before passing them to Tk.
  1423. */
  1424.  
  1425. static int
  1426. PyMacConvertEvent(eventPtr)
  1427.     EventRecord *eventPtr;
  1428. {
  1429.   if (SIOUXHandleOneEvent(eventPtr))
  1430.     return 0; /* Nothing happened to the Tcl event queue */
  1431.   return TkMacConvertEvent(eventPtr);
  1432. }
  1433.  
  1434. #if GENERATINGCFM
  1435.  
  1436. /*
  1437. ** Additional Mac specific code for dealing with shared libraries.
  1438. */
  1439.  
  1440. #include <Resources.h>
  1441. #include <CodeFragments.h>
  1442.  
  1443. static int loaded_from_shlib = 0;
  1444. static FSSpec library_fss;
  1445.  
  1446. /*
  1447. ** If this module is dynamically loaded the following routine should
  1448. ** be the init routine. It takes care of adding the shared library to
  1449. ** the resource-file chain, so that the tk routines can find their
  1450. ** resources.
  1451. */
  1452. OSErr pascal
  1453. init_tkinter_shlib(InitBlockPtr data)
  1454. {
  1455.     __initialize();
  1456.     if ( data == nil ) return noErr;
  1457.     if ( data->fragLocator.where == kOnDiskFlat ) {
  1458.         library_fss = *data->fragLocator.u.onDisk.fileSpec;
  1459.         loaded_from_shlib = 1;
  1460.     } else if ( data->fragLocator.where == kOnDiskSegmented ) {
  1461.         library_fss = *data->fragLocator.u.inSegs.fileSpec;
  1462.         loaded_from_shlib = 1;
  1463.     }
  1464.     return noErr;
  1465. }
  1466.  
  1467. /*
  1468. ** Insert the library resources into the search path. Put them after
  1469. ** the resources from the application. Again, we ignore errors.
  1470. */
  1471. static
  1472. mac_addlibresources()
  1473. {
  1474.     if ( !loaded_from_shlib ) 
  1475.         return;
  1476.     (void)FSpOpenResFile(&library_fss, fsRdPerm);
  1477. }
  1478.  
  1479. #endif /* GENERATINGCFM */
  1480. #endif /* macintosh */
  1481.